package com.itheima.day15.fanshe2;

import java.lang.reflect.Constructor;

public class Test {
    public static void main(String[] args) throws Exception {
        Class c = Cat.class;
        //获取构造方法,只能获取public修饰的构造方法
        Constructor[] constructors = c.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
        System.out.println("--------------------------------");
        //获取所有构造方法
        Constructor[] constructors1 = c.getDeclaredConstructors();
        for (Constructor constructor : constructors1) {
            System.out.println(constructor);
        }
        System.out.println("--------------------------------");
        //获取指定构造方法
        Constructor constructor = c.getDeclaredConstructor(String.class, int.class);
        System.out.println(constructor);
        System.out.println("--------------------------------");
        //获取指定构造方法2
        Constructor constructor1 = c.getDeclaredConstructor(int.class);
        System.out.println(constructor1);
        //用Class获取的构造方法创建Cat对象
        constructor.setAccessible(true);
        Object cat =  constructor.newInstance("Tom", 5);
        System.out.println(cat);
    }
}
